package com.youjia.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youjia.mapper.ItemCatMapper;
import com.youjia.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;

    public Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
        for (ItemCat itemCat : itemCatList) {
            Integer parentId = itemCat.getParentId();
            if(!map.containsKey(parentId)) {
                List<ItemCat> list = new ArrayList<>();
                list.add(itemCat);
                map.put(parentId, list);
            } else {
                map.get(parentId).add(itemCat);
            }
        }
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer, List<ItemCat>> map = getMap();
        if(level == 1) {
            return map.get(0);
        }
        if(level == 2) {
            return findTwoItemCatList(map);
        }
        return findThreeItemCatList(map);
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        itemCatMapper.insert(itemCat.setStatus(true));
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
//        if(itemCat.getLevel() == 3) {
//            itemCatMapper.deleteById(itemCat.getId());
//        }
        if(itemCat.getLevel() == 2) {
            itemCatMapper.delete(new QueryWrapper<ItemCat>().
                    eq("parent_id", itemCat.getId()));
            itemCatMapper.deleteById(itemCat.getId());
        }
        if(itemCat.getLevel() == 1) {
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", itemCat.getId());
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for (ItemCat twoItemCat : twoList) {
                queryWrapper.clear();
                itemCatMapper.delete(queryWrapper.eq("parent_id", twoItemCat.getId()));
                itemCatMapper.deleteById(twoItemCat.getId());
            }
            itemCatMapper.deleteById(itemCat.getId());
        }
    }

    private List<ItemCat> findThreeItemCatList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = findTwoItemCatList(map);
        for (ItemCat oneItemCat : oneList) {
            List<ItemCat> twoList = oneItemCat.getChildren();
            if(twoList != null) {
                for (ItemCat twoItemCat : twoList) {
                    twoItemCat.setChildren(map.get(twoItemCat.getId()));
                }
            }
        }
        return oneList;
    }

    private List<ItemCat> findTwoItemCatList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        for (ItemCat oneItemCat : oneList) {
            oneItemCat.setChildren(map.get(oneItemCat.getId()));
        }
        return oneList;
    }
}
